
var xiangmumc = document.getElementById('xiangmumc');
if(xiangmumc.value==null || xiangmumc.value==""){
	alert('项目需分类申请，网上审批结束后，由采购中心将《采购项目任务书》交计财处审核，审核通过后，由采购中心操作执行。');
}

$(function (){	
	autocomplete($('input.fuzerenxm'), "gonghaocxServlet", convertData, function(event, ui){$("#fuzerengh").val(ui.item.id)});
    
});


$("#gonghuosj").cgCalendar({showTime:false,format:"yyyy-mm-dd"});

//提示js---start
window.ljtips=function(){var i=document.body,j=document.documentElement,n=/msie\s([\d\.]+)/.test(navigator.userAgent.toLowerCase()),k=window;return function(l){var c=l?typeof l=="string"?document.getElementById(l):l:this,d=null,s=c.offsetWidth,t=c.offsetHeight,m=0,g=null,e={},o={left:function(a){return{top:d.top,left:d.left-a-15}},top:function(a,h){return{top:d.top-h-15,left:d.left}},right:function(){return{top:d.top,left:d.left+s+15}},bottom:function(){return{top:d.top+t+15,left:d.left}}};e.show=
function(a){if(c.lock)c.lock=false;else{c.lock=true;d=c.getBoundingClientRect();var h=i.scrollTop+j.scrollTop,f=i.scrollLeft+j.scrollLeft;a.p=a.p||"right";var b=e.append(a.p,a.closeBtn||false);b.getElementsByTagName("DIV")[0].innerHTML=a.content;var p=o[a.p](b.offsetWidth,b.offsetHeight);b.style.top=p.top+h+"px";b.style.left=p.left+f+"px";a.time&&setTimeout(function(){e.clear(document.getElementById("lj"+m))},a.time);a.fn&&a.fn.call(c,document.getElementById("lj"+m));g=function(w,q){return function(){var u=
i.scrollTop+j.scrollTop,v=i.scrollLeft+j.scrollLeft;d=c.getBoundingClientRect();var r=o[a.p](b.offsetWidth,b.offsetHeight);q.style.top=r.top+u+"px";q.style.left=r.left+v+"px"}}(c,b);n?k.attachEvent("onresize",g):k.addEventListener("resize",g,false)}};e.append=function(a,h){var f=m=Math.floor(Math.random()*1E4),b=document.createElement("DIV");b.id="lj"+f;b.innerHTML='<div class="lj-tipsWrap" id="tipsWrap-<%=r%>"><div ></div><span class="lj-in lj-<%=p%>"><span class="lj-in"></span></span><a href="javascript:void(0)" id="ljClose<%=r%>" class="lj-close">x</a></div>'.replace("<%=p%>",
a).replace(/<%=r%>/g,f);document.body.appendChild(b);if(h)document.getElementById("ljClose"+f).onclick=e.hide;else document.getElementById("ljClose"+f).style.display="none";return document.getElementById("tipsWrap-"+f)};e.clear=function(a){a&&a.parentNode&&a.parentNode.removeChild(a);n?k.detachEvent("onresize",g):k.removeEventListener("resize",g,false);c.lock=false};e.hide=function(){e.clear(document.getElementById("lj"+m))};return e}}();

var test1 = ljtips('jingfeimc');
document.getElementById("jingfeimc").onfocus = function(){test1.show({content:"友情提示：应由学校经费支出的采购项目，请填写其经费名称为“校教学设备购置费”，经费代码为“0031008-02”。",p:'top'})};
document.getElementById("jingfeimc").onblur = function(){test1.hide()};
var test2 = ljtips('beizhu1');
document.getElementById("beizhu1").onfocus = function(){test2.show({content:"备注说明限十个字以内，如字数太多，请将说明纸质材料提交给实验室与资产管理处宁萍。",p:'top'})};
document.getElementById("beizhu1").onblur = function(){
	test2.hide();
	var content = this.parentNode.parentNode.childNodes[3].getElementsByTagName("input")[0].value;

	if(content.indexOf("空调")>=0){
		if(this.value==""){
			alert("购买空调时备注不能为空");
			this.focus();
			return false;
		}
		return true;
	}
	};
//提示js---end



$(document).ready(function(){
	var edit = $("#edit").val();
	var sqid = $("#sqid").val();
	if(edit == 0&& sqid){
		$(":text").attr('disabled', 'disabled');
		$("#addrow").attr('disabled', 'disabled');
		$("select").attr('disabled', 'disabled');
		$("textarea").attr('disabled', 'disabled');
		$(":checkbox").attr('disabled', 'disabled');
	}
	$("#reedit").click(function(){
		$("#reedit").css("display", "none");
		$("#submit2").css("display", "inline");
		$("#edit").val(1);
		$(":text").removeAttr('disabled');
		$("#addrow").removeAttr('disabled');
		$("select").removeAttr('disabled');
		$("textarea").removeAttr('disabled');
		$(":checkbox").removeAttr('disabled');
	});

});

//验证表单
function tosubmit(){

	if(validate_form(form1) != false){
		document.form1.submit();
	}
}

function validate_required(field,alerttxt)
{
if(field.tagName=="SELECT"){

	v=field.options[field.selectedIndex].value;

	 if (!v||v==0)
		{alert(alerttxt);return false}
	  else {return true}
	  
}else{
	with (field)
  {
  if (value==null||value=="")
    {alert(alerttxt);return false}
  else {return true}
  }
  }
}

function validate_format(field,alerttxt)
{
with (field)
  {
  var patrn=/^\w+-.+$/;
  if (!patrn.exec(value))
    {alert(alerttxt);return false}
  else {return true}
  }
}
function validate_format_jiage(field,alerttxt)
{
with (field)
  {
  var patrn=/^(\d+|(\d+\.\d+))$/;
  if (!patrn.exec(value))
    {alert(alerttxt);return false}
  else {return true}
  }
}
function validate_form(thisform)
{
	var oTable = document.getElementById("tbl");
	var oTr = oTable.getElementsByTagName("tr");
	for(var i = 1;i<oTr.length;i++)
	{
		var length = oTr[i].getElementsByTagName("td").length;
		//alert(oTr[i].getElementsByTagName("td")[length-1].firstChild.value);
		checkBeizhu(oTr[i].getElementsByTagName("td")[length-1].firstChild);
	}
	
with (thisform)
  {

  if (validate_required(lianxidh,"联系电话（手机）不能为空!")==false)
    {lianxidh.focus();return false}
		
 if (validate_required(dw_id,"请选择使用部门!")==false)
    {dw_id.focus();return false} 
  
  if (validate_required(gonghuosj,"供货时间不能为空!")==false)
    {gonghuosj.focus();return false}
  
  if (validate_required(jingfeimc,"经费名称不能为空!")==false)
    {jingfeimc.focus();return false}

  if (validate_format_jiage(yusuanje,"预算金额不能为空且应为数字!")==false)
    {yusuanje.focus();return false}
  
  if (validate_required(jingfeidm,"经费代码不能为空!")==false)
    {jingfeidm.focus();return false}
  
  if (validate_required(fuzerenxm,"项目负责人姓名不能为空!")==false)
    {fuzerenxm.focus();return false}
  if (validate_format(fuzerenxm,"项目负责人的输入方式为：1111-某某!")==false)
    {fuzerenxm.focus();return false}
  
  if (validate_required(mingcheng1,"名称不能为空!")==false)
    {mingcheng1.focus();return false}
  
  if (validate_required(miaoshu1,"描述不能为空!")==false)
    {miaoshu1.focus();return false}
  
  if (validate_required(danwei1,"单位不能为空!")==false)
    {danwei1.focus();return false}
  
  if (validate_required(shuliang1,"数量不能为空!")==false)
    {shuliang1.focus();return false}
  
  if (validate_format_jiage(danjia1,"单价不能为空且应为数字!")==false)
    {danjia1.focus();return false}
  
  if (validate_format_jiage(hejia1,"合价不能为空且应为数字!")==false)
    {hejia1.focus();return false}
  
  //if (validate_required(beizhu1,"备注不能为空!")==false)
  //  {beizhu1.focus();return false}
  
  if (validate_format_jiage(hejije,"合计金额不能为空且为数字!")==false)
    {hejije.focus();return false}

  if(hejije.value == 0)
  {hejije.focus(); alert("合计金额不能为0");return false}
  }

	

}
//表格自动添加行数
function addRowToTable()
{
var tbl = document.getElementById('tbl');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
row.setAttribute('align', 'center');
// cell 0
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
var elid = document.createElement('input');  //id
elid.type = 'hidden';
elid.name = 'id' + iteration;
elid.id = 'id' + iteration;
elid.onkeypress = keyPressTest;
cellLeft.appendChild(textNode);
cellLeft.appendChild(elid);
// cell 1
var cellRight = row.insertCell(1);
var elmc = document.createElement('input');
elmc.type = 'text';
elmc.name = 'mingcheng' + iteration;
elmc.id = 'mingcheng' + iteration;
elmc.size = 10;
elmc.onkeypress = keyPressTest;
cellRight.appendChild(elmc);
// cell 2
var cellRight = row.insertCell(2);
var elms = document.createElement('textarea');
elms.cols = 25;
elms.name = 'miaoshu' + iteration;
elms.id = 'miaoshu' + iteration;
elms.rows = 5;
elms.onkeypress = keyPressTest;
cellRight.appendChild(elms);
// cell 3
var cellRight = row.insertCell(3);
var eldw = document.createElement('input');
eldw.type = 'text';
eldw.name = 'danwei' + iteration;
eldw.id = 'danwei' + iteration;
eldw.size = 2;
eldw.onkeypress = keyPressTest;
cellRight.appendChild(eldw);
// cell 4
var cellRight = row.insertCell(4);
var elsl = document.createElement('input');
elsl.type = 'text';
elsl.name = 'shuliang' + iteration;
elsl.id = 'shuliang' + iteration;
elsl.value = "0";
elsl.size = 5;
elsl.onkeypress = keyPressTest;
cellRight.appendChild(elsl);
// cell 5
var cellRight = row.insertCell(5);
var eldj = document.createElement('input');
eldj.type = 'text';
elsl.value = "0";
eldj.name = 'danjia' + iteration;
eldj.id = 'danjia' + iteration;
eldj.size = 7;
eldj.onkeypress = keyPressTest;
cellRight.appendChild(eldj);
// cell 6
var cellRight = row.insertCell(6);
var elhj = document.createElement('input');
elhj.type = 'text';
elsl.value = "0";
elhj.name = 'hejia' + iteration;
elhj.id = 'hejia' + iteration;
elhj.size = 7;
elhj.onkeypress = keyPressTest;
elhj.onblur=autoCount;
cellRight.appendChild(elhj);
// cell 7
var cellRight = row.insertCell(7);
var elbz = document.createElement('input');
elbz.type = 'text';
elbz.name = 'beizhu' + iteration;
elbz.id = 'beizhu' + iteration;
elbz.size = 15;
elbz.onblur = checkBeizhu;
elbz.onkeypress = keyPressTest;
cellRight.appendChild(elbz);
//修改num值
document.getElementById('num').value= iteration;
}
function keyPressTest(e, obj)
{
var validateChkb = document.getElementById('chkValidateOnKeyPress');
if (validateChkb.checked) {
var displayObj = document.getElementById('spanOutput');
var key;
if(window.event) {
key = window.event.keyCode;
}
else if(e.which) {
key = e.which;
}
var objId;
if (obj != null) {
objId = obj.id;
} else {
objId = this.id;
}
displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
}
}
//表格自动添加行数end	
 function autoCount()
 {
		$(this).val(parseFloat($(this).parent().prev().find("input").eq(0).val())*parseInt($(this).parent().prev().prev().find("input").eq(0).val()));
		
		var count = 0;
		$("#tbl tr").each(function(index){		
			if(index!=0)
			{
				count = count+parseFloat($(this).find("input").eq(length-2).val());
			}
		});
		$("#hejije").val(count);
 }
 
 function checkBeizhu(obj)
 {
	 var name ;
	 if(obj){
		 name = obj;		 
	 }else{
		 name = this;
	 }
	var content = name.parentNode.parentNode.childNodes[3].getElementsByTagName("input")[0].value;

	if(content.indexOf("空调")>=0){
		if(name.value==""){
			alert("购买空调时备注不能为空");
			this.focus();
			return false;
		}
		return true;
	}
 }
 
 function checkBlur(obj)
 {
	 var name ;
	 if(obj){
		 name = obj;
	 }else{
		 name = this;
	 }

     name.value=order_num(name.value);;
     
     var count = 0.00;
     
     var oTr = name.parentNode.parentNode;

     var oNum = oTr.getElementsByTagName("td")[4].getElementsByTagName('input')[0];
     var oHj =  oTr.getElementsByTagName("td")[6].getElementsByTagName('input')[0];
         
     //  alert(name.value);
     //alert(oNum.value);
    // alert(name.value);value*100)*parseFloat(name.parentNode.nextSibling.getelementsbytagname('input')[0].value)/100);
    oHj.value = order_num(parseFloat(name.value*100)*parseFloat(oNum.value)/100);
     
	$("#tbl tr").each(function(index){		
			if(index!=0)
			{
				count = parseFloat(count)+parseFloat($(this).find("input").eq(length-2).val());
			}
		});

	
    

	$("#hejije").val(order_num(count));
 }
 
 function order_num(num)
 {
	 num += '';
	    num = num.replace(/[^0-9|\.]/g, ''); //清除字符串非数字非.字符
	    
	    if(/^0+/) //清除字符串0
	        num = num.replace(/^0+/, '');
	    if(!/\./.test(num)) //整数字符串末尾添加.00
	        num += '.00';
	    if(/^\./.test(num)) //字符.,添加0
	        num = '0' + num;
	    num += '00';        //字符串末尾补零
	    num = num.match(/\d+\.\d{2}/)[0];
	    
	    return num;
 }
 
